<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="70px">
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantId"
            placeholder="请输入工厂"
            clearable
            @keyup.enter="search"
            @change="closeDialog(searchParam.plantId)"
            filterable
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心">
          <el-select
            v-model="searchParam.workCenterId"
            placeholder="请选择工作中心"
            :disabled="searchParam.plantId === ''"
            clearable
            @keyup.enter="search"
            filterable
          >
            <el-option
              v-for="item in workshopList"
              :key="item.id"
              :label="`${item.workshopCode}-${item.workshopName}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="计划年份">
          <el-input
            v-model="searchParam.planYear"
            placeholder="请输入计划年份"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="计划周数">
          <el-input
            v-model="searchParam.planWeek"
            placeholder="请输入计划周数"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="计划状态">
          <el-select v-model="searchParam.planState" clearable>
            <el-option
              v-for="item in useDictionary('ProduceOrderState')"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
          <el-button
            class="ml-3"
            @click="
              useDownload('/mes/mesproduceplan/export', '生产计划', searchParam)
            "
          >
            <template #icon><i-ep-download /></template>导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!-- <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template> -->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="生产计划编号"
          prop="producePlanCode"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="工厂"
          prop="plantCode"
          width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="160"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ scope.row.workCenterCode }} - {{ scope.row.workCenterName }}
          </template>
        </el-table-column>
        <el-table-column
          label="计划开始日期"
          prop="planBeginDate"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="计划截止日期"
          prop="planEndDate"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="物料"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划数量"
          prop="planQuantity"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="已完成数量"
          prop="planFinishQuantity"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ useDictionary("MeasurementUnit", scope.row.quantityUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          label="计划状态"
          prop="planState"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            <el-tag>
              {{ useDictionary("ProduceOrderState", scope.row.planState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="计划年份"
          prop="planYear"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="计划周数"
          prop="planWeek"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <!-- 
        <el-table-column label="生产状态" prop="planState" min-width="100">
          <template #default="scope">
            {{ useDictionary("ProduceOrderState", scope.row.planState) }}
          </template>
        </el-table-column>

        <el-table-column
          label="计划批次"
          prop="planBatch"
          min-width="100"
          show-overflow-tooltip
        /> -->

        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesproduceplan-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary, useDownload } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESPRODUCEPLAN",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesproduceplan/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

//工厂列表
const plantList = ref([]);
const getPlantList = async () => {
  const { data } = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = data;
};
function closeDialog(plantId: String) {
  getWorkshopList(plantId);
}
//工作中心
const workshopList = ref([]);
const getWorkshopList = async (plantId: String) => {
  const { data } = await request({
    url: "mes/mesworkshop/getList",
    method: "post",
    data: { plantId: plantId },
  });
  workshopList.value = data;
};
// 初始化搜索参数数据
const searchParamInit = {
  plantCode: "",
  workCenterCode: "",
  planYear: "",
  planWeek: "",
};

const deleteParam = {
  api: "/mes/mesproduceplan/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlantList();
});

let plants = ref([]);
const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesproduceplan/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
}
/** 打开生产计划表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改生产计划";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增生产计划";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开生产计划表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "生产计划详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
